{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#chap10a  Python网络与数据库程序设计26\n",
    "#####UDP通信程序，\n",
    "#发送端发送一个字符串或信息\n",
    "#接收端在计算机的5005端口进行接收\n",
    "\n",
    "#发送端代码：sender.py \n",
    "#接收端代码：receiver.py\n",
    "#以发送bye结束"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1、接收端监听发送端的端口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n#  1st type in \\n# cd ../ds/> \\n\\npython receiver.py\\n'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "#  1st type in \n",
    "# cd ../ds/> \n",
    "\n",
    "python receiver.py\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2、发送端返送数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n#  2nd type in \\n#cd ../ds/> \\npython sender.py hello\\npython sender.py 12345\\npython sender.py bye\\n'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "#  2nd type in \n",
    "#cd ../ds/> \n",
    "python sender.py hello\n",
    "python sender.py 12345\n",
    "python sender.py bye\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#####TCP通信程序\n",
    "#Must be 3.5 !!!!!\n",
    "#发送端代码：server.py \n",
    "#接收端代码：client.py\n",
    "\n",
    "#Better lauch two CMD wnd\n",
    "# cd ../ds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n#  1st type in \\n# pythons/py35LessonPlusSpiderML/chp07> \\npython client.py\\n'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "#  1st type in \n",
    "python client.py\n",
    "'''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n#  2nd type in \\n# pythons/pyLesson/chp09> \\npython server.py\\n\\n#End with Client input  bye\\n'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "#  2nd type in \n",
    "python server.py\n",
    "\n",
    "#End with Client input  bye\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 端口扫描程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#嗅探器程序需要管理员权限\n",
    "#运行程序  python sniffer.py   #Protocol not supported!\n",
    "#运行端口扫描程序   python port_scan.py\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "starting 10.2.1.8...\n",
      "starting 10.2.1.9...\n"
     ]
    }
   ],
   "source": [
    "import socket\n",
    "import multiprocessing\n",
    "\n",
    "def ports(ports_service):\n",
    "    #获取常用端口对应的服务名称\n",
    "    for port in list(range(1,100))+[143,145,113,443,445,3389, 8080]:\n",
    "        try:\n",
    "            ports_service[port] = socket.getservbyport(port)\n",
    "        except socket.error:\n",
    "            pass\n",
    "\n",
    "def ports_scan(host, ports_service):\n",
    "    ports_open = []\n",
    "    try:\n",
    "        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
    "        sock.settimeout(0.1)\n",
    "    except socket.error:\n",
    "        print('socket creation error')\n",
    "        sys.exit()\n",
    "    for port in ports_service:\n",
    "        try:\n",
    "            #尝试连接指定端口\n",
    "            sock.connect((host,port))\n",
    "            #记录打开的端口\n",
    "            ports_open.append(port)\n",
    "            sock.close()\n",
    "        except socket.error:\n",
    "            pass\n",
    "    return ports_open\n",
    "\n",
    "if __name__=='__main__':\n",
    "    m = multiprocessing.Manager()\n",
    "    ports_service = dict()\n",
    "    results = dict()\n",
    "    ports(ports_service)\n",
    "    #创建进程池，允许最多8个进程同时运行\n",
    "    pool = multiprocessing.Pool(processes=8)\n",
    "    net = '10.2.1.'\n",
    "    for host_number in map(str,range(8,10)):\n",
    "        host = net+host_number\n",
    "        #创建一个新进程，同时记录其运行结果\n",
    "        results[host] = pool.apply_async(ports_scan, (host, ports_service))\n",
    "        print('starting '+host+'...')\n",
    "    #关闭进程池，close()必须在join()之前调用\n",
    "    pool.close()\n",
    "    #等待进程池中的进程全部执行结束\n",
    "    pool.join()\n",
    "\n",
    "    #打印输出结果\n",
    "    for host in results:\n",
    "        print('='*30)\n",
    "        print(host,'.'*10)\n",
    "        for port in results[host].get():\n",
    "            print(port, ':', ports_service[port])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
